namespace ScrumSprintMonitor.ServerAdapter.TFS.TemplateAdaptors
{
	#region #using Directives

	using System;

	using Infrastructure;

	using Isolation.TeamFoundationServer;

	#endregion

	public class CodePlexWorkItemAdapter : CoreWorkItemAdapter
	{
		public CodePlexWorkItemAdapter(IWorkItem workItem)
			: base(workItem)
		{
		}

		protected override void RetrieveType()
		{
			switch ((string) WorkItem["CodeStudio.WorkItemType"])
			{
				case "Task":
					Type = WorkItemType.SprintBacklogItem;
					break;
				case "Issue":
					Type = WorkItemType.Defect;
					break;
				case "Feature":
					Type = WorkItemType.UserStory;
					break;
				default:
					Type = WorkItemType.Unknown;
					break;
			}
		}

		protected override void RetrieveState()
		{
			switch (WorkItem.State)
			{
				case "Closed":
				case "Fixed":
					State = WorkItemState.Done;
					break;
				case "Active":
					State = WorkItemState.InProgress;
					break;
				default:
					State = WorkItemState.NotDone;
					break;
			}
		}

		protected override void RetrieveRemainingWork()
		{
			if (Type == WorkItemType.SprintBacklogItem)
			{
				var remainingWork = WorkItem["CodePlex.Custom"];
				if (remainingWork == null)
				{
					RemainingWork = double.NaN;
				}
				else if (remainingWork as string == string.Empty)
				{
					RemainingWork = 0.0;
				}
				else
				{
					RemainingWork = Convert.ToDouble(remainingWork);
				}
			}
			else
			{
				RemainingWork = 0.0;
			}
		}

		protected override void RetrieveDoneWork()
		{
			DoneWork = 0.0;
		}
	}
}